package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.biz.TransOutboundInvoker;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.constant.OrderItemConstants;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.Stock;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.OrderCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OrderCoreServiceImpl1 implements OrderCoreService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderProcessPipelineFactory orderProcessPipelineFactory;

    @Autowired
    StockMapper stockMapper;


    /**
     * 创建订单的处理流程
     *
     * @param request
     * @return
     */
    @Override
    public String createOrder(CreateOrderRequest request) {
        //创建pipeline对象
        TransOutboundInvoker invoker = orderProcessPipelineFactory.build(request);

        //启动pipeline
        invoker.start(); //启动流程（pipeline来处理）

        //获取处理结果
        AbsTransHandlerContext context = invoker.getContext();

        //从Context中获取订单Id
        String orderId = context.getOrderId();
        return orderId;
    }

    @Override
    public void cancelOrder(String orderId) {
        //更新order表中的订单状态和更新时间
        Order order = new Order();
        order.setStatus(OrderConstants.ORDER_STATUS_TRANSACTION_CANCEL);
        order.setUpdateTime(new Date());
        order.setOrderId(orderId);
        orderMapper.updateOrderByOrderId(order);

        //更新order_item表中的 状态码 和 更新时间
        int status = OrderItemConstants.STOCK_RELEASE_CODE;
        Date updateTime = new Date();
        orderItemMapper.updateOrderItemByOrderId(status,updateTime,orderId);

        //更新stock中被锁住的数量。
        //先查询orderid对应的所有的商品的id和数量,
        List<OrderItem> orderItems = orderItemMapper.selectOrderItemByOrderId(orderId);
        for (OrderItem orderItem : orderItems) {
            Stock stock = new Stock();
            stock.setItemId(orderItem.getItemId());
            stock.setLockCount(-orderItem.getNum());
            stock.setStockCount(orderItem.getNum().longValue());
            stockMapper.updateStock(stock);
        }
    }

    @Override
    public void deleteOrder(String orderId) {
//先获取order表中的order的status，判断是否需要更新stock表中的数据
        //如果order表中的status是0（初始化状态/未付款）则需要更新stock表中的数据
        //如果是其他（已付款之后的状态），不需要更新stock表
        //然后获取orderItem表中的item的数据
        //更新stock表
        //删除orderitem表中的orderid对应的数据
        //删除order表中的orderid对应的数据
        String status = orderMapper.selectStatusByOrderId(orderId);
        List<OrderItem> orderItems = orderItemMapper.selectOrderItemByOrderId(orderId);
        if ("0".equals(status)){
            for (OrderItem orderItem : orderItems) {
                Stock stock = new Stock();
                stock.setItemId(orderItem.getItemId());
                stock.setLockCount(-orderItem.getNum());
                stock.setStockCount(orderItem.getNum().longValue());
                stockMapper.updateStock(stock);
            }
        }
        orderItemMapper.deleteItemByOrderId(orderId);
        orderMapper.deleteOrderByOrderId(orderId);
    }

    @Override
    public void updateOrder(Integer status, String orderId) {
    }

}
